package com.example.tianqi;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class weather_listAdapter extends RecyclerView.Adapter<weather_listAdapter.ViewHolder> {

    private final ArrayList<JSONObject> arrayList;

    public weather_listAdapter(ArrayList<JSONObject> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        //在此处 写 每一个子项需要执行的操作
        JSONObject jsonObject = arrayList.get(position);
        try {
            holder.dayTextview.setText(jsonObject.getString("date"));
            holder.dayTextview.append(" " + jsonObject.getString("week"));
            holder.heat2TextViewt.setText(jsonObject.getString("tem2"));
            holder.heat2TextViewt.append("~" + jsonObject.getString("tem1"));
            holder.weatherTextView.append(jsonObject.getString("wea"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_weather_list, group, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final AppCompatTextView dayTextview;
        private final AppCompatTextView heat2TextViewt;
        private final AppCompatTextView weatherTextView;

        private ViewHolder(View view) {
            super(view);
            dayTextview = view.findViewById(R.id.dayTextView);
            heat2TextViewt = view.findViewById(R.id.heat2TextViewt);
            weatherTextView = view.findViewById(R.id.weatherTextView);
        }
    }

}
